home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / rmailsum.el.z / rmailsum.el
Encoding:
Text File  |  1998-10-28  |  53.0 KB  |  1,481 lines

  1. ;;; rmailsum.el --- make summary buffers for the mail reader
  2.  
  3. ;; Copyright (C) 1985, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: mail
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Extended by Bob Weiner of Motorola
  28. ;;   Provided all commands from rmail-mode in rmail-summary-mode and made key
  29. ;;   bindings in both modes wholly compatible.
  30.  
  31. ;;; Code:
  32.  
  33. ;; For rmail-select-summary
  34. (require 'rmail)
  35.  
  36. ;;;###autoload
  37. (defvar rmail-summary-scroll-between-messages t
  38.   "*Non-nil means Rmail summary scroll commands move between messages.")
  39.  
  40. (defvar rmail-summary-font-lock-keywords
  41.   '(("^....D.*" . font-lock-string-face)            ; Deleted.
  42.     ("^....-.*" . font-lock-type-face)                ; Unread.
  43.     ;; Neither of the below will be highlighted if either of the above are:
  44.     ("^....[^D-] \\(......\\)" 1 font-lock-keyword-face)    ; Date.
  45.     ("{ \\([^}]+\\),}" 1 font-lock-comment-face))        ; Labels.
  46.   "Additional expressions to highlight in Rmail Summary mode.")
  47.  
  48. ;; Entry points for making a summary buffer.
  49.  
  50. ;; Regenerate the contents of the summary
  51. ;; using the same selection criterion as last time.
  52. ;; M-x revert-buffer in a summary buffer calls this function.
  53. (defun rmail-update-summary (&rest ignore)
  54.   (apply (car rmail-summary-redo) (cdr rmail-summary-redo)))
  55.  
  56. (defun rmail-summary ()
  57.   "Display a summary of all messages, one line per message."
  58.   (interactive)
  59.   (rmail-new-summary "All" '(rmail-summary) nil))
  60.  
  61. (defun rmail-summary-by-labels (labels)
  62.   "Display a summary of all messages with one or more LABELS.
  63. LABELS should be a string containing the desired labels, separated by commas."
  64.   (interactive "sLabels to summarize by: ")
  65.   (if (string= labels "")
  66.       (setq labels (or rmail-last-multi-labels
  67.                (error "No label specified"))))
  68.   (setq rmail-last-multi-labels labels)
  69.   (rmail-new-summary (concat "labels " labels)
  70.              (list 'rmail-summary-by-labels labels)
  71.              'rmail-message-labels-p
  72.              (concat ", \\(" (mail-comma-list-regexp labels) "\\),")))
  73.  
  74. (defun rmail-summary-by-recipients (recipients &optional primary-only)
  75.   "Display a summary of all messages with the given RECIPIENTS.
  76. Normally checks the To, From and Cc fields of headers;
  77. but if PRIMARY-ONLY is non-nil (prefix arg given),
  78.  only look in the To and From fields.
  79. RECIPIENTS is a string of regexps separated by commas."
  80.   (interactive "sRecipients to summarize by: \nP")
  81.   (rmail-new-summary
  82.    (concat "recipients " recipients)
  83.    (list 'rmail-summary-by-recipients recipients primary-only)
  84.    'rmail-message-recipients-p
  85.    (mail-comma-list-regexp recipients) primary-only))
  86.  
  87. (defun rmail-summary-by-regexp (regexp)
  88.   "Display a summary of all messages according to regexp REGEXP.
  89. If the regular expression is found in the header of the message
  90. \(including in the date and other lines, as well as the subject line),
  91. Emacs will list the header line in the RMAIL-summary."
  92.   (interactive "sRegexp to summarize by: ")
  93.   (if (string= regexp "")
  94.       (setq regexp (or rmail-last-regexp
  95.              (error "No regexp specified."))))
  96.   (setq rmail-last-regexp regexp)
  97.   (rmail-new-summary (concat "regexp " regexp)
  98.              (list 'rmail-summary-by-regexp regexp)
  99.              'rmail-message-regexp-p
  100.                      regexp))
  101.  
  102. ;; rmail-summary-by-topic
  103. ;; 1989 R.A. Schnitzler
  104.  
  105. (defun rmail-summary-by-topic (subject &optional whole-message)
  106.   "Display a summary of all messages with the given SUBJECT.
  107. Normally checks the Subject field of headers;
  108. but if WHOLE-MESSAGE is non-nil (prefix arg given), 
  109.  look in the whole message.
  110. SUBJECT is a string of regexps separated by commas."
  111.   (interactive "sTopics to summarize by: \nP")
  112.   (rmail-new-summary
  113.    (concat "about " subject)
  114.    (list 'rmail-summary-by-topic subject whole-message)
  115.    'rmail-message-subject-p
  116.    (mail-comma-list-regexp subject) whole-message))
  117.  
  118. (defun rmail-message-subject-p (msg subject &optional whole-message)
  119.   (save-restriction
  120.     (goto-char (rmail-msgbeg msg))
  121.     (search-forward "\n*** EOOH ***\n")
  122.     (narrow-to-region
  123.      (point)
  124.      (progn (search-forward (if whole-message "\^_" "\n\n")) (point)))
  125.     (goto-char (point-min))
  126.     (if whole-message (re-search-forward subject nil t)
  127.       (string-match subject (or (mail-fetch-field "Subject") "")) )))
  128.  
  129. (defun rmail-summary-by-senders (senders)
  130.   "Display a summary of all messages with the given SENDERS.
  131. SENDERS is a string of names separated by commas."
  132.   (interactive "sSenders to summarize by: ")
  133.   (rmail-new-summary
  134.    (concat "senders " senders)
  135.    (list 'rmail-summary-by-senders senders)
  136.    'rmail-message-senders-p
  137.    (mail-comma-list-regexp senders)))
  138.  
  139. (defun rmail-message-senders-p (msg senders)
  140.   (save-restriction
  141.     (goto-char (rmail-msgbeg msg))
  142.     (search-forward "\n*** EOOH ***\n")
  143.     (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
  144.     (string-match senders (or (mail-fetch-field "From") ""))))
  145.  
  146. ;; General making of a summary buffer.
  147.  
  148. (defvar rmail-summary-symbol-number 0)
  149.  
  150. (defun rmail-new-summary (description redo-form function &rest args)
  151.   "Create a summary of selected messages.
  152. DESCRIPTION makes part of the mode line of the summary buffer.
  153. For each message, FUNCTION is applied to the message number and ARGS...
  154. and if the result is non-nil, that message is included.
  155. nil for FUNCTION means all messages."
  156.   (message "Computing summary lines...")
  157.   (let (sumbuf mesg was-in-summary)
  158.     (save-excursion
  159.       ;; Go to the Rmail buffer.
  160.       (if (eq major-mode 'rmail-summary-mode)
  161.       (progn
  162.         (setq was-in-summary t)
  163.         (set-buffer rmail-buffer)))
  164.       ;; Find its summary buffer, or make one.
  165.       (setq sumbuf
  166.         (if (and rmail-summary-buffer
  167.              (buffer-name rmail-summary-buffer))
  168.         rmail-summary-buffer
  169.           (generate-new-buffer (concat (buffer-name) "-summary"))))
  170.       (setq mesg rmail-current-message)
  171.       ;; Filter the messages; make or get their summary lines.
  172.       (let ((summary-msgs ())
  173.         (new-summary-line-count 0))
  174.     (let ((msgnum 1)
  175.           (buffer-read-only nil)
  176.           (old-min (point-min-marker))
  177.           (old-max (point-max-marker)))
  178.       ;; Can't use save-restriction here; that doesn't work if we
  179.       ;; plan to modify text outside the original restriction.
  180.       (save-excursion
  181.         (widen)
  182.         (goto-char (point-min))
  183.         (while (>= rmail-total-messages msgnum)
  184.           (if (or (null function)
  185.               (apply function (cons msgnum args)))
  186.           (setq summary-msgs
  187.             (cons (cons msgnum (rmail-make-summary-line msgnum))
  188.                   summary-msgs)))
  189.           (setq msgnum (1+ msgnum)))
  190.         (setq summary-msgs (nreverse summary-msgs)))
  191.       (narrow-to-region old-min old-max))
  192.     ;; Temporarily, while summary buffer is unfinished,
  193.     ;; we "don't have" a summary.
  194.     (setq rmail-summary-buffer nil)
  195.     (save-excursion
  196.       (let ((rbuf (current-buffer))
  197.         (total rmail-total-messages))
  198.         (set-buffer sumbuf)
  199.         ;; Set up the summary buffer's contents.
  200.         (let ((buffer-read-only nil))
  201.           (erase-buffer)
  202.           (while summary-msgs
  203.         (princ (cdr (car summary-msgs)) sumbuf)
  204.         (setq summary-msgs (cdr summary-msgs)))
  205.           (goto-char (point-min)))
  206.         ;; Set up the rest of its state and local variables.
  207.         (setq buffer-read-only t)
  208.         (rmail-summary-mode)
  209.         (make-local-variable 'minor-mode-alist)
  210.         (setq minor-mode-alist (list (list t (concat ": " description))))
  211.         (setq rmail-buffer rbuf
  212.           rmail-summary-redo redo-form
  213.           rmail-total-messages total))))
  214.       (setq rmail-summary-buffer sumbuf))
  215.     ;; Now display the summary buffer and go to the right place in it.
  216.     (or was-in-summary
  217.     (progn
  218.       (if (and (one-window-p)
  219.            pop-up-windows (not pop-up-frames))
  220.           ;; If there is just one window, put the summary on the top.
  221.           (progn
  222.         (split-window (selected-window) rmail-summary-window-size)
  223.         (select-window (next-window (frame-first-window)))
  224.         (pop-to-buffer sumbuf)
  225.         ;; If pop-to-buffer did not use that window, delete that
  226.         ;; window.  (This can happen if it uses another frame.)
  227.         (if (not (eq sumbuf (window-buffer (frame-first-window))))
  228.             (delete-other-windows)))
  229.         (pop-to-buffer sumbuf))
  230.       (set-buffer rmail-buffer)
  231.       ;; This is how rmail makes the summary buffer reappear.
  232.       ;; We do this here to make the window the proper size.
  233.       (rmail-select-summary nil)
  234.       (set-buffer rmail-summary-buffer)))
  235.     (rmail-summary-goto-msg mesg t t)
  236.     (rmail-summary-construct-io-menu)
  237.     (message "Computing summary lines...done")))
  238.  
  239. ;; Low levels of generating a summary.
  240.  
  241. (defun rmail-make-summary-line (msg)
  242.   (let ((line (or (aref rmail-summary-vector (1- msg))
  243.           (progn
  244.             (setq new-summary-line-count
  245.               (1+ new-summary-line-count))
  246.             (if (zerop (% new-summary-line-count 10))
  247.             (message "Computing summary lines...%d"
  248.                  new-summary-line-count))
  249.             (rmail-make-summary-line-1 msg)))))
  250.     ;; Fix up the part of the summary that says "deleted" or "unseen".
  251.     (aset line 4
  252.       (if (rmail-message-deleted-p msg) ?\D
  253.         (if (= ?0 (char-after (+ 3 (rmail-msgbeg msg))))
  254.         ?\- ?\ )))
  255.     line))
  256.  
  257. (defun rmail-make-summary-line-1 (msg)
  258.   (goto-char (rmail-msgbeg msg))
  259.   (let* ((lim (save-excursion (forward-line 2) (point)))
  260.      pos
  261.      (labels
  262.       (progn
  263.         (forward-char 3)
  264.         (concat
  265. ;         (if (save-excursion (re-search-forward ",answered," lim t))
  266. ;         "*" "")
  267. ;         (if (save-excursion (re-search-forward ",filed," lim t))
  268. ;         "!" "")
  269.          (if (progn (search-forward ",,") (eolp))
  270.          ""
  271.            (concat "{"
  272.                (buffer-substring (point)
  273.                      (progn (end-of-line) (point)))
  274.                "} ")))))
  275.      (line
  276.       (progn
  277.         (forward-line 1)
  278.         (if (looking-at "Summary-line: ")
  279.         (progn
  280.           (goto-char (match-end 0))
  281.           (setq line
  282.             (buffer-substring (point)
  283.                       (progn (forward-line 1) (point)))))))))
  284.     ;; Obsolete status lines lacking a # should be flushed.
  285.     (and line
  286.      (not (string-match "#" line))
  287.      (progn
  288.        (delete-region (point)
  289.               (progn (forward-line -1) (point)))
  290.        (setq line nil)))
  291.     ;; If we didn't get a valid status line from the message,
  292.     ;; make a new one and put it in the message.
  293.     (or line
  294.     (let* ((case-fold-search t)
  295.            (next (rmail-msgend msg))
  296.            (beg (if (progn (goto-char (rmail-msgbeg msg))
  297.                    (search-forward "\n*** EOOH ***\n" next t))
  298.             (point)
  299.               (forward-line 1)
  300.               (point)))
  301.            (end (progn (search-forward "\n\n" nil t) (point))))
  302.       (save-restriction
  303.         (narrow-to-region beg end)
  304.         (goto-char beg)
  305.         (setq line (rmail-make-basic-summary-line)))
  306.       (goto-char (rmail-msgbeg msg))
  307.       (forward-line 2)
  308.       (insert "Summary-line: " line)))
  309.     (setq pos (string-match "#" line))
  310.     (aset rmail-summary-vector (1- msg)
  311.       (concat (format "%4d  " msg)
  312.           (substring line 0 pos)
  313.           labels
  314.           (substring line (1+ pos))))))
  315.  
  316. (defun rmail-make-basic-summary-line ()
  317.   (goto-char (point-min))
  318.   (concat (save-excursion
  319.         (if (not (re-search-forward "^Date:" nil t))
  320.         "      "
  321.           (cond ((re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
  322.               (save-excursion (end-of-line) (point)) t)
  323.              (format "%2d-%3s"
  324.                  (string-to-int (buffer-substring
  325.                          (match-beginning 2)
  326.                          (match-end 2)))
  327.                  (buffer-substring
  328.                   (match-beginning 4) (match-end 4))))
  329.             ((re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
  330.               (save-excursion (end-of-line) (point)) t)
  331.              (format "%2d-%3s"
  332.                  (string-to-int (buffer-substring
  333.                          (match-beginning 4)
  334.                          (match-end 4)))
  335.                  (buffer-substring
  336.                   (match-beginning 2) (match-end 2))))
  337.             ((re-search-forward "\\(19\\|20\\)\\([0-9][0-9]\\)-\\([01][0-9]\\)-\\([0-3][0-9]\\)"
  338.               (save-excursion (end-of-line) (point)) t)
  339.              (format "%2s%2s%2s"
  340.                  (buffer-substring
  341.                   (match-beginning 2) (match-end 2))
  342.                  (buffer-substring
  343.                   (match-beginning 3) (match-end 3))
  344.                  (buffer-substring
  345.                   (match-beginning 4) (match-end 4))))
  346.             (t "??????"))))
  347.       "  "
  348.       (save-excursion
  349.         (if (not (re-search-forward "^From:[ \t]*" nil t))
  350.         "                         "
  351.           (let* ((from (mail-strip-quoted-names
  352.                 (buffer-substring
  353.                  (1- (point))
  354.                  ;; Get all the lines of the From field
  355.                  ;; so that we get a whole comment if there is one,
  356.                  ;; so that mail-strip-quoted-names can discard it.
  357.                  (let ((opoint (point)))
  358.                    (while (progn (forward-line 1)
  359.                          (looking-at "[ \t]")))
  360.                    ;; Back up over newline, then trailing spaces or tabs
  361.                    (forward-char -1)
  362.                    (skip-chars-backward " \t")
  363.                    (point)))))
  364.                      len mch lo)
  365.         (if (string-match (concat "^\\("
  366.                       (regexp-quote (user-login-name))
  367.                       "\\($\\|@\\)\\|"
  368.                       (regexp-quote
  369.                        ;; Don't lose if run from init file
  370.                        ;; where user-mail-address is not
  371.                        ;; set yet.
  372.                        (or user-mail-address
  373.                            (concat (user-login-name) "@"
  374.                                (or mail-host-address
  375.                                (system-name)))))
  376.                       "\\>\\)")
  377.                   from)
  378.             (save-excursion
  379.               (goto-char (point-min))
  380.               (if (not (re-search-forward "^To:[ \t]*" nil t))
  381.               nil
  382.             (setq from
  383.                   (concat "to: "
  384.                       (mail-strip-quoted-names
  385.                        (buffer-substring
  386.                     (point)
  387.                     (progn (end-of-line)
  388.                            (skip-chars-backward " \t")
  389.                            (point)))))))))
  390.         (setq len (length from))
  391.         (setq mch (string-match "[@%]" from))
  392.         (format "%25s"
  393.             (if (or (not mch) (<= len 25))
  394.                 (substring from (max 0 (- len 25)))
  395.               (substring from
  396.                      (setq lo (cond ((< (- mch 14) 0) 0)
  397.                             ((< len (+ mch 11))
  398.                              (- len 25))
  399.                             (t (- mch 14))))
  400.                      (min len (+ lo 25))))))))
  401.       (save-excursion
  402.         (save-restriction
  403.           (widen)
  404.           (let ((beg (rmail-msgbeg msgnum))
  405.             (end (rmail-msgend msgnum))
  406.             lines)
  407.         (save-excursion
  408.           (goto-char beg)
  409.           ;; Count only lines in the reformatted header,
  410.           ;; if we have reformatted it.
  411.           (search-forward "\n*** EOOH ***\n" end t)
  412.           (setq lines (count-lines (point) end)))
  413.         (format (cond
  414.              ((<= lines     9) "   [%d]")
  415.              ((<= lines    99) "  [%d]")
  416.              ((<= lines   999) " [%3d]")
  417.              (t            "[%d]"))
  418.             lines))))
  419.       " #"                ;The # is part of the format.
  420.       (if (re-search-forward "^Subject:" nil t)
  421.           (progn (skip-chars-forward " \t")
  422.              (buffer-substring (point)
  423.                        (progn (end-of-line)
  424.                           (point))))
  425.         (re-search-forward "[\n][\n]+" nil t)
  426.         (buffer-substring (point) (progn (end-of-line) (point))))
  427.       "\n"))
  428.  
  429. ;; Simple motion in a summary buffer.
  430.  
  431. (defun rmail-summary-next-all (&optional number)
  432.   (interactive "p")
  433.   (forward-line (if number number 1))
  434.   ;; It doesn't look nice to move forward past the last message line.
  435.   (and (eobp) (> number 0)
  436.        (forward-line -1))
  437.   (display-buffer rmail-buffer))
  438.  
  439. (defun rmail-summary-previous-all (&optional number)
  440.   (interactive "p")
  441.   (forward-line (- (if number number 1)))
  442.   ;; It doesn't look nice to move forward past the last message line.
  443.   (and (eobp) (< number 0)
  444.        (forward-line -1))
  445.   (display-buffer rmail-buffer))
  446.  
  447. (defun rmail-summary-next-msg (&optional number)
  448.   "Display next non-deleted msg from rmail file.
  449. With optional prefix argument NUMBER, moves forward this number of non-deleted
  450. messages, or backward if NUMBER is negative."
  451.   (interactive "p")
  452.   (forward-line 0)
  453.   (and (> number 0) (end-of-line))
  454.   (let ((count (if (< number 0) (- number) number))
  455.     (search (if (> number 0) 're-search-forward 're-search-backward))
  456.     (non-del-msg-found nil))
  457.     (while (and (> count 0) (setq non-del-msg-found
  458.                   (or (funcall search "^....[^D]" nil t)
  459.                       non-del-msg-found)))
  460.       (setq count (1- count))))
  461.   (beginning-of-line)
  462.   (display-buffer rmail-buffer))
  463.  
  464. (defun rmail-summary-previous-msg (&optional number)
  465.   (interactive "p")
  466.   (rmail-summary-next-msg (- (if number number 1))))
  467.  
  468. (defun rmail-summary-next-labeled-message (n labels)
  469.   "Show next message with LABEL.  Defaults to last labels used.
  470. With prefix argument N moves forward N messages with these labels."
  471.   (interactive "p\nsMove to next msg with labels: ")
  472.   (let (msg)
  473.     (save-excursion
  474.       (set-buffer rmail-buffer)
  475.       (rmail-next-labeled-message n labels)
  476.       (setq msg rmail-current-message))
  477.     (rmail-summary-goto-msg msg)))
  478.  
  479. (defun rmail-summary-previous-labeled-message (n labels)
  480.   "Show previous message with LABEL.  Defaults to last labels used.
  481. With prefix argument N moves backward N messages with these labels."
  482.   (interactive "p\nsMove to previous msg with labels: ")
  483.   (let (msg)
  484.     (save-excursion
  485.       (set-buffer rmail-buffer)
  486.       (rmail-previous-labeled-message n labels)
  487.       (setq msg rmail-current-message))
  488.     (rmail-summary-goto-msg msg)))
  489.  
  490. (defun rmail-summary-next-same-subject (n)
  491.   "Go to the next message in the summary having the same subject.
  492. With prefix argument N, do this N times.
  493. If N is negative, go backwards."
  494.   (interactive "p")
  495.   (let (subject search-regexp  i found
  496.     (forward (> n 0)))
  497.     (save-excursion
  498.       (set-buffer rmail-buffer)
  499.       (setq subject (mail-fetch-field "Subject"))
  500.       (setq search-regexp (concat "^Subject: *\\(Re: *\\)?"
  501.                   (regexp-quote subject)
  502.                   "\n"))
  503.       (setq i rmail-current-message))
  504.     (if (string-match "Re:[ \t]*" subject)
  505.     (setq subject (substring subject (match-end 0))))
  506.     (save-excursion
  507.       (while (and (/= n 0)
  508.           (if forward
  509.               (not (eobp))
  510.             (not (bobp))))
  511.     (let (done)
  512.       (while (and (not done)
  513.               (if forward
  514.               (not (eobp))
  515.             (not (bobp))))
  516.         ;; Advance thru summary.
  517.         (forward-line (if forward 1 -1))
  518.         ;; Get msg number of this line.
  519.         (setq i (string-to-int
  520.              (buffer-substring (point)
  521.                        (min (point-max) (+ 5 (point))))))
  522.         ;; See if that msg has desired subject.
  523.         (save-excursion
  524.           (set-buffer rmail-buffer)
  525.           (save-restriction
  526.         (widen)
  527.         (goto-char (rmail-msgbeg i))
  528.         (search-forward "\n*** EOOH ***\n")
  529.         (let ((beg (point)) end)
  530.           (search-forward "\n\n")
  531.           (setq end (point))
  532.           (goto-char beg)
  533.           (setq done (re-search-forward search-regexp end t))))))
  534.       (if done (setq found i)))
  535.     (setq n (if forward (1- n) (1+ n)))))
  536.     (if found
  537.     (rmail-summary-goto-msg found)
  538.       (error "No %s message with same subject"
  539.          (if forward "following" "previous")))))
  540.  
  541. (defun rmail-summary-previous-same-subject (n)
  542.   "Go to the previous message in the summary having the same subject.
  543. With prefix argument N, do this N times.
  544. If N is negative, go forwards instead."
  545.   (interactive "p")
  546.   (rmail-summary-next-same-subject (- n)))
  547.  
  548. ;; Delete and undelete summary commands.
  549.  
  550. (defun rmail-summary-delete-forward (&optional backward)
  551.   "Delete this message and move to next nondeleted one.
  552. Deleted messages stay in the file until the \\[rmail-expunge] command is given.
  553. With prefix argument, delete and move backward."
  554.   (interactive "P")
  555.   (let (end)
  556.     (rmail-summary-goto-msg)
  557.     (pop-to-buffer rmail-buffer)
  558.     (rmail-delete-message)
  559.     (let ((del-msg rmail-current-message))
  560.       (pop-to-buffer rmail-summary-buffer)
  561.       (rmail-summary-mark-deleted del-msg)
  562.       (while (and (not (if backward (bobp) (eobp)))
  563.           (save-excursion (beginning-of-line)
  564.                   (looking-at " *[0-9]+D")))
  565.     (forward-line (if backward -1 1)))
  566.       ;; It looks ugly to move to the empty line at end of buffer.
  567.       (and (eobp) (not backward)
  568.        (forward-line -1)))))
  569.  
  570. (defun rmail-summary-delete-backward ()
  571.   "Delete this message and move to previous nondeleted one.
  572. Deleted messages stay in the file until the \\[rmail-expunge] command is given."
  573.   (interactive)
  574.   (rmail-summary-delete-forward t))
  575.  
  576. (defun rmail-summary-mark-deleted (&optional n undel)
  577.   ;; Since third arg is t, this only alters the summary, not the Rmail buf.
  578.   (and n (rmail-summary-goto-msg n t t))
  579.   (or (eobp)
  580.       (not (overlay-get rmail-summary-overlay 'face))
  581.       (let ((buffer-read-only nil))
  582.     (skip-chars-forward " ")
  583.     (skip-chars-forward "[0-9]")
  584.     (if undel
  585.         (if (looking-at "D")
  586.         (progn (delete-char 1) (insert " ")))
  587.       (delete-char 1)
  588.       (insert "D"))))
  589.   (beginning-of-line))
  590.  
  591. (defun rmail-summary-mark-undeleted (n)
  592.   (rmail-summary-mark-deleted n t))
  593.  
  594. (defun rmail-summary-deleted-p (&optional n)
  595.   (save-excursion
  596.     (and n (rmail-summary-goto-msg n nil t))
  597.     (skip-chars-forward " ")
  598.     (skip-chars-forward "[0-9]")
  599.     (looking-at "D")))
  600.  
  601. (defun rmail-summary-undelete (&optional arg)
  602.   "Undelete current message.
  603. Optional prefix ARG means undelete ARG previous messages."
  604.   (interactive "p")
  605.   (if (/= arg 1)
  606.       (rmail-summary-undelete-many arg)
  607.     (let ((buffer-read-only nil)
  608.       (opoint (point)))
  609.       (end-of-line)
  610.       (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
  611.          (replace-match "\\1 ")
  612.          (rmail-summary-goto-msg)
  613.          (pop-to-buffer rmail-buffer)
  614.          (and (rmail-message-deleted-p rmail-current-message)
  615.           (rmail-undelete-previous-message))
  616.          (pop-to-buffer rmail-summary-buffer))
  617.         (t (goto-char opoint))))))
  618.  
  619. (defun rmail-summary-undelete-many (&optional n)
  620.   "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
  621.   (interactive "P")
  622.   (save-excursion
  623.     (set-buffer rmail-buffer)
  624.     (let* ((init-msg (if n rmail-current-message rmail-total-messages))
  625.        (rmail-current-message init-msg)
  626.        (n (or n rmail-total-messages))
  627.        (msgs-undeled 0))
  628.       (while (and (> rmail-current-message 0)
  629.           (< msgs-undeled n))
  630.     (if (rmail-message-deleted-p rmail-current-message)
  631.         (progn (rmail-set-attribute "deleted" nil)
  632.            (setq msgs-undeled (1+ msgs-undeled))))
  633.     (setq rmail-current-message (1- rmail-current-message)))
  634.       (set-buffer rmail-summary-buffer)
  635.       (setq rmail-current-message init-msg msgs-undeled 0)
  636.       (while (and (> rmail-current-message 0)
  637.           (< msgs-undeled n))
  638.     (if (rmail-summary-deleted-p rmail-current-message)
  639.         (progn (rmail-summary-mark-undeleted rmail-current-message)
  640.            (setq msgs-undeled (1+ msgs-undeled))))
  641.     (setq rmail-current-message (1- rmail-current-message))))
  642.     (rmail-summary-goto-msg)))
  643.  
  644. ;; Rmail Summary mode is suitable only for specially formatted data.
  645. (put 'rmail-summary-mode 'mode-class 'special)
  646.  
  647. (defun rmail-summary-mode ()
  648.   "Rmail Summary Mode is invoked from Rmail Mode by using \\<rmail-mode-map>\\[rmail-summary].
  649. As commands are issued in the summary buffer, they are applied to the
  650. corresponding mail messages in the rmail buffer.
  651.  
  652. All normal editing commands are turned off.
  653. Instead, nearly all the Rmail mode commands are available,
  654. though many of them move only among the messages in the summary.
  655.  
  656. These additional commands exist:
  657.  
  658. \\[rmail-summary-undelete-many]    Undelete all or prefix arg deleted messages.
  659. \\[rmail-summary-wipe] Delete the summary and go to the Rmail buffer.
  660.  
  661. Commands for sorting the summary:
  662.  
  663. \\[rmail-summary-sort-by-date] Sort by date.
  664. \\[rmail-summary-sort-by-subject] Sort by subject.
  665. \\[rmail-summary-sort-by-author] Sort by author.
  666. \\[rmail-summary-sort-by-recipient] Sort by recipient.
  667. \\[rmail-summary-sort-by-correspondent] Sort by correspondent.
  668. \\[rmail-summary-sort-by-lines] Sort by lines.
  669. \\[rmail-summary-sort-by-keywords] Sort by keywords."
  670.   (interactive)
  671.   (kill-all-local-variables)
  672.   (setq major-mode 'rmail-summary-mode)
  673.   (setq mode-name "RMAIL Summary")
  674.   (setq truncate-lines t)
  675.   (setq buffer-read-only t)
  676.   (set-syntax-table text-mode-syntax-table)
  677.   (make-local-variable 'rmail-buffer)
  678.   (make-local-variable 'rmail-total-messages)
  679.   (make-local-variable 'rmail-current-message)
  680.   (setq rmail-current-message nil)
  681.   (make-local-variable 'rmail-summary-redo)
  682.   (setq rmail-summary-redo nil)
  683.   (make-local-variable 'revert-buffer-function)
  684.   (make-local-hook 'post-command-hook)
  685.   (make-local-variable 'font-lock-defaults)
  686.   (setq font-lock-defaults '(rmail-summary-font-lock-keywords t))
  687.   (rmail-summary-enable)
  688.   (run-hooks 'rmail-summary-mode-hook))
  689.  
  690. ;; Summary features need to be disabled during edit mode.
  691. (defun rmail-summary-disable ()
  692.   (use-local-map text-mode-map)
  693.   (remove-hook 'post-command-hook 'rmail-summary-rmail-update t)
  694.   (setq revert-buffer-function nil))
  695.  
  696. (defun rmail-summary-enable ()
  697.   (use-local-map rmail-summary-mode-map)
  698.   (add-hook 'post-command-hook 'rmail-summary-rmail-update nil t)
  699.   (setq revert-buffer-function 'rmail-update-summary))
  700.  
  701. ;; Show in Rmail the message described by the summary line that point is on,
  702. ;; but only if the Rmail buffer is already visible.
  703. ;; This is a post-command-hook in summary buffers.
  704. (defun rmail-summary-rmail-update ()
  705.   (let (buffer-read-only)
  706.     (save-excursion
  707.       ;; If at end of buffer, pretend we are on the last text line.
  708.       (if (eobp)
  709.       (forward-line -1))
  710.       (beginning-of-line)
  711.       (skip-chars-forward " ")
  712.       (let ((msg-num (string-to-int (buffer-substring
  713.                      (point)
  714.                      (progn (skip-chars-forward "0-9")
  715.                         (point))))))
  716.     (or (eq rmail-current-message msg-num)
  717.         (let ((window (get-buffer-window rmail-buffer))
  718.           (owin (selected-window)))
  719.           (setq rmail-current-message msg-num)
  720.           (if (= (following-char) ?-)
  721.           (progn
  722.             (delete-char 1)
  723.             (insert " ")))
  724.           (if window
  725.           ;; Using save-window-excursion would cause the new value
  726.           ;; of point to get lost.
  727.           (unwind-protect
  728.               (progn
  729.             (select-window window)
  730.             (rmail-show-message msg-num t))
  731.             (select-window owin))
  732.         (if (buffer-name rmail-buffer)
  733.             (save-excursion
  734.               (set-buffer rmail-buffer)
  735.               (rmail-show-message msg-num t))))))
  736.     (rmail-summary-update-highlight nil)))))
  737.  
  738. (defvar rmail-summary-mode-map nil)
  739.  
  740. (if rmail-summary-mode-map
  741.     nil
  742.   (setq rmail-summary-mode-map (make-keymap))
  743.   (suppress-keymap rmail-summary-mode-map)
  744.   (define-key rmail-summary-mode-map "a"      'rmail-summary-add-label)
  745.   (define-key rmail-summary-mode-map "b"      'rmail-summary-bury)
  746.   (define-key rmail-summary-mode-map "c"      'rmail-summary-continue)
  747.   (define-key rmail-summary-mode-map "d"      'rmail-summary-delete-forward)
  748.   (define-key rmail-summary-mode-map "\C-d"   'rmail-summary-delete-backward)
  749.   (define-key rmail-summary-mode-map "e"      'rmail-summary-edit-current-message)
  750.   (define-key rmail-summary-mode-map "f"      'rmail-summary-forward)
  751.   (define-key rmail-summary-mode-map "g"      'rmail-summary-get-new-mail)
  752.   (define-key rmail-summary-mode-map "h"      'rmail-summary)
  753.   (define-key rmail-summary-mode-map "i"      'rmail-summary-input)
  754.   (define-key rmail-summary-mode-map "j"      'rmail-summary-goto-msg)
  755.   (define-key rmail-summary-mode-map "k"      'rmail-summary-kill-label)
  756.   (define-key rmail-summary-mode-map "l"      'rmail-summary-by-labels)
  757.   (define-key rmail-summary-mode-map "\e\C-h" 'rmail-summary)
  758.   (define-key rmail-summary-mode-map "\e\C-l" 'rmail-summary-by-labels)
  759.   (define-key rmail-summary-mode-map "\e\C-r" 'rmail-summary-by-recipients)
  760.   (define-key rmail-summary-mode-map "\e\C-s" 'rmail-summary-by-regexp)
  761.   (define-key rmail-summary-mode-map "\e\C-t" 'rmail-summary-by-topic)
  762.   (define-key rmail-summary-mode-map "m"      'rmail-summary-mail)
  763.   (define-key rmail-summary-mode-map "\M-m"   'rmail-summary-retry-failure)
  764.   (define-key rmail-summary-mode-map "n"      'rmail-summary-next-msg)
  765.   (define-key rmail-summary-mode-map "\en"    'rmail-summary-next-all)
  766.   (define-key rmail-summary-mode-map "\e\C-n" 'rmail-summary-next-labeled-message)
  767.   (define-key rmail-summary-mode-map "o"      'rmail-summary-output-to-rmail-file)
  768.   (define-key rmail-summary-mode-map "\C-o"   'rmail-summary-output)
  769.   (define-key rmail-summary-mode-map "p"      'rmail-summary-previous-msg)
  770.   (define-key rmail-summary-mode-map "\ep"    'rmail-summary-previous-all)
  771.   (define-key rmail-summary-mode-map "\e\C-p" 'rmail-summary-previous-labeled-message)
  772.   (define-key rmail-summary-mode-map "q"      'rmail-summary-quit)
  773.   (define-key rmail-summary-mode-map "r"      'rmail-summary-reply)
  774.   (define-key rmail-summary-mode-map "s"      'rmail-summary-expunge-and-save)
  775.   (define-key rmail-summary-mode-map "\es"    'rmail-summary-search)
  776.   (define-key rmail-summary-mode-map "t"      'rmail-summary-toggle-header)
  777.   (define-key rmail-summary-mode-map "u"      'rmail-summary-undelete)
  778.   (define-key rmail-summary-mode-map "\M-u"   'rmail-summary-undelete-many)
  779.   (define-key rmail-summary-mode-map "w"      'rmail-summary-wipe)
  780.   (define-key rmail-summary-mode-map "x"      'rmail-summary-expunge)
  781.   (define-key rmail-summary-mode-map "."      'rmail-summary-beginning-of-message)
  782.   (define-key rmail-summary-mode-map "<"      'rmail-summary-first-message)
  783.   (define-key rmail-summary-mode-map ">"      'rmail-summary-last-message)
  784.   (define-key rmail-summary-mode-map " "      'rmail-summary-scroll-msg-up)
  785.   (define-key rmail-summary-mode-map "\177"   'rmail-summary-scroll-msg-down)
  786.   (define-key rmail-summary-mode-map "?"      'describe-mode)
  787.   (define-key rmail-summary-mode-map "\C-c\C-n" 'rmail-summary-next-same-subject)
  788.   (define-key rmail-summary-mode-map "\C-c\C-p" 'rmail-summary-previous-same-subject)
  789.   (define-key rmail-summary-mode-map "\C-c\C-s\C-d"
  790.     'rmail-summary-sort-by-date)
  791.   (define-key rmail-summary-mode-map "\C-c\C-s\C-s"
  792.     'rmail-summary-sort-by-subject)
  793.   (define-key rmail-summary-mode-map "\C-c\C-s\C-a"
  794.     'rmail-summary-sort-by-author)
  795.   (define-key rmail-summary-mode-map "\C-c\C-s\C-r"
  796.     'rmail-summary-sort-by-recipient)
  797.   (define-key rmail-summary-mode-map "\C-c\C-s\C-c"
  798.     'rmail-summary-sort-by-correspondent)
  799.   (define-key rmail-summary-mode-map "\C-c\C-s\C-l"
  800.     'rmail-summary-sort-by-lines)
  801.   (define-key rmail-summary-mode-map "\C-c\C-s\C-k"
  802.     'rmail-summary-sort-by-keywords)
  803.   )
  804.  
  805. ;;; Menu bar bindings.
  806.  
  807. (define-key rmail-summary-mode-map [menu-bar] (make-sparse-keymap))
  808.  
  809. (define-key rmail-summary-mode-map [menu-bar classify]
  810.   (cons "Classify" (make-sparse-keymap "Classify")))
  811.  
  812. (define-key rmail-summary-mode-map [menu-bar classify output-menu]
  813.   '("Output (Rmail Menu)..." . rmail-summary-output-menu))
  814.  
  815. (define-key rmail-summary-mode-map [menu-bar classify input-menu]
  816.   '("Input Rmail File (menu)..." . rmail-input-menu))
  817.  
  818. (define-key rmail-summary-mode-map [menu-bar classify input-menu]
  819.   '(nil))
  820.  
  821. (define-key rmail-summary-mode-map [menu-bar classify output-menu]
  822.   '(nil))
  823.  
  824. (define-key rmail-summary-mode-map [menu-bar classify output-inbox]
  825.   '("Output (inbox)..." . rmail-summary-output))
  826.  
  827. (define-key rmail-summary-mode-map [menu-bar classify output]
  828.   '("Output (Rmail)..." . rmail-summary-output-to-rmail-file))
  829.  
  830. (define-key rmail-summary-mode-map [menu-bar classify kill-label]
  831.   '("Kill Label..." . rmail-summary-kill-label))
  832.  
  833. (define-key rmail-summary-mode-map [menu-bar classify add-label]
  834.   '("Add Label..." . rmail-summary-add-label))
  835.  
  836. (define-key rmail-summary-mode-map [menu-bar summary]
  837.   (cons "Summary" (make-sparse-keymap "Summary")))
  838.  
  839. (define-key rmail-summary-mode-map [menu-bar summary senders]
  840.   '("By Senders..." . rmail-summary-by-senders))
  841.  
  842. (define-key rmail-summary-mode-map [menu-bar summary labels]
  843.   '("By Labels..." . rmail-summary-by-labels))
  844.  
  845. (define-key rmail-summary-mode-map [menu-bar summary recipients]
  846.   '("By Recipients..." . rmail-summary-by-recipients))
  847.  
  848. (define-key rmail-summary-mode-map [menu-bar summary topic]
  849.   '("By Topic..." . rmail-summary-by-topic))
  850.  
  851. (define-key rmail-summary-mode-map [menu-bar summary regexp]
  852.   '("By Regexp..." . rmail-summary-by-regexp))
  853.  
  854. (define-key rmail-summary-mode-map [menu-bar summary all]
  855.   '("All" . rmail-summary))
  856.  
  857. (define-key rmail-summary-mode-map [menu-bar mail]
  858.   (cons "Mail" (make-sparse-keymap "Mail")))
  859.  
  860. (define-key rmail-summary-mode-map [menu-bar mail rmail-summary-get-new-mail]
  861.   '("Get New Mail" . rmail-summary-get-new-mail))
  862.  
  863. (define-key rmail-summary-mode-map [menu-bar mail lambda]
  864.   '("----"))
  865.  
  866. (define-key rmail-summary-mode-map [menu-bar mail continue]
  867.   '("Continue" . rmail-summary-continue))
  868.  
  869. (define-key rmail-summary-mode-map [menu-bar mail resend]
  870.   '("Re-send..." . rmail-summary-resend))
  871.  
  872. (define-key rmail-summary-mode-map [menu-bar mail forward]
  873.   '("Forward" . rmail-summary-forward))
  874.  
  875. (define-key rmail-summary-mode-map [menu-bar mail retry]
  876.   '("Retry" . rmail-summary-retry-failure))
  877.  
  878. (define-key rmail-summary-mode-map [menu-bar mail reply]
  879.   '("Reply" . rmail-summary-reply))
  880.  
  881. (define-key rmail-summary-mode-map [menu-bar mail mail]
  882.   '("Mail" . rmail-summary-mail))
  883.  
  884. (define-key rmail-summary-mode-map [menu-bar delete]
  885.   (cons "Delete" (make-sparse-keymap "Delete")))
  886.  
  887. (define-key rmail-summary-mode-map [menu-bar delete expunge/save]
  888.   '("Expunge/Save" . rmail-summary-expunge-and-save))
  889.  
  890. (define-key rmail-summary-mode-map [menu-bar delete expunge]
  891.   '("Expunge" . rmail-summary-expunge))
  892.  
  893. (define-key rmail-summary-mode-map [menu-bar delete undelete]
  894.   '("Undelete" . rmail-summary-undelete))
  895.  
  896. (define-key rmail-summary-mode-map [menu-bar delete delete]
  897.   '("Delete" . rmail-summary-delete-forward))
  898.  
  899. (define-key rmail-summary-mode-map [menu-bar move]
  900.   (cons "Move" (make-sparse-keymap "Move")))
  901.  
  902. (define-key rmail-summary-mode-map [menu-bar move search-back]
  903.   '("Search Back..." . rmail-summary-search-backward))
  904.  
  905. (define-key rmail-summary-mode-map [menu-bar move search]
  906.   '("Search..." . rmail-summary-search))
  907.  
  908. (define-key rmail-summary-mode-map [menu-bar move previous]
  909.   '("Previous Nondeleted" . rmail-summary-previous-msg))
  910.  
  911. (define-key rmail-summary-mode-map [menu-bar move next]
  912.   '("Next Nondeleted" . rmail-summary-next-msg))
  913.  
  914. (define-key rmail-summary-mode-map [menu-bar move last]
  915.   '("Last" . rmail-summary-last-message))
  916.  
  917. (define-key rmail-summary-mode-map [menu-bar move first]
  918.   '("First" . rmail-summary-first-message))
  919.  
  920. (define-key rmail-summary-mode-map [menu-bar move previous]
  921.   '("Previous" . rmail-summary-previous-all))
  922.  
  923. (define-key rmail-summary-mode-map [menu-bar move next]
  924.   '("Next" . rmail-summary-next-all))
  925.  
  926. (defvar rmail-summary-overlay nil)
  927. (put 'rmail-summary-overlay 'permanent-local t)
  928.  
  929. ;; Go to message N in the summary buffer which is current,
  930. ;; and in the corresponding Rmail buffer.
  931. ;; If N is nil, use the message corresponding to point in the summary
  932. ;; and move to that message in the Rmail buffer.
  933.  
  934. ;; If NOWARN, don't say anything if N is out of range.
  935. ;; If SKIP-RMAIL, don't do anything to the Rmail buffer.
  936.  
  937. (defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
  938.   (interactive "P")
  939.   (if (consp n) (setq n (prefix-numeric-value n)))
  940.   (if (eobp) (forward-line -1))
  941.   (beginning-of-line)
  942.   (let* ((obuf (current-buffer))
  943.      (buf rmail-buffer)
  944.      (cur (point))
  945.      message-not-found
  946.      (curmsg (string-to-int
  947.           (buffer-substring (point)
  948.                     (min (point-max) (+ 5 (point))))))
  949.      (total (save-excursion (set-buffer buf) rmail-total-messages)))
  950.     ;; If message number N was specified, find that message's line
  951.     ;; or set message-not-found.
  952.     ;; If N wasn't specified or that message can't be found.
  953.     ;; set N by default.
  954.     (if (not n)
  955.     (setq n curmsg)
  956.       (if (< n 1)
  957.       (progn (message "No preceding message")
  958.          (setq n 1)))
  959.       (if (> n total)
  960.       (progn (message "No following message")
  961.          (goto-char (point-max))
  962.          (rmail-summary-goto-msg nil nowarn skip-rmail)))
  963.       (goto-char (point-min))
  964.       (if (not (re-search-forward (format "^%4d[^0-9]" n) nil t))
  965.       (progn (or nowarn (message "Message %d not found" n))
  966.          (setq n curmsg)
  967.          (setq message-not-found t)
  968.          (goto-char cur))))
  969.     (beginning-of-line)
  970.     (skip-chars-forward " ")
  971.     (skip-chars-forward "0-9")
  972.     (save-excursion (if (= (following-char) ?-)
  973.             (let ((buffer-read-only nil))
  974.               (delete-char 1)
  975.               (insert " "))))
  976.     (rmail-summary-update-highlight message-not-found)
  977.     (beginning-of-line)
  978.     (if skip-rmail
  979.     nil
  980.       (let ((selwin (selected-window)))
  981.     (unwind-protect
  982.         (progn (pop-to-buffer buf)
  983.            (rmail-show-message n))
  984.       (select-window selwin)
  985.       ;; The actions above can alter the current buffer.  Preserve it.
  986.       (set-buffer obuf))))))
  987.  
  988. ;; Update the highlighted line in an rmail summary buffer.
  989. ;; That should be current.  We highlight the line point is on.
  990. ;; If NOT-FOUND is non-nil, we turn off highlighting.
  991. (defun rmail-summary-update-highlight (not-found)
  992.   ;; Make sure we have an overlay to use.
  993.   (or rmail-summary-overlay
  994.       (progn
  995.     (make-local-variable 'rmail-summary-overlay)
  996.     (setq rmail-summary-overlay (make-overlay (point) (point)))))
  997.   ;; If this message is in the summary, use the overlay to highlight it.
  998.   ;; Otherwise, don't highlight anything.
  999.   (if not-found
  1000.       (overlay-put rmail-summary-overlay 'face nil)
  1001.     (move-overlay rmail-summary-overlay
  1002.           (save-excursion (beginning-of-line)
  1003.                   (skip-chars-forward " ")
  1004.                   (point))
  1005.           (save-excursion (end-of-line) (point)))
  1006.     (overlay-put rmail-summary-overlay 'face 'highlight)))
  1007.  
  1008. (defun rmail-summary-scroll-msg-up (&optional dist)
  1009.   "Scroll the Rmail window forward.
  1010. If the Rmail window is displaying the end of a message,
  1011. advance to the next message."
  1012.   (interactive "P")
  1013.   (if (eq dist '-)
  1014.       (rmail-summary-scroll-msg-down nil)
  1015.     (let ((rmail-buffer-window (get-buffer-window rmail-buffer)))
  1016.       (if rmail-buffer-window
  1017.       (if (let ((rmail-summary-window (selected-window)))
  1018.         (select-window rmail-buffer-window)
  1019.         (prog1
  1020.             ;; Is EOB visible in the buffer?
  1021.             (save-excursion
  1022.               (let ((ht (window-height (selected-window))))
  1023.             (move-to-window-line (- ht 2))
  1024.             (end-of-line)
  1025.             (eobp)))
  1026.           (select-window rmail-summary-window)))
  1027.           (if (not rmail-summary-scroll-between-messages)
  1028.           (error "End of buffer")
  1029.         (rmail-summary-next-msg (or dist 1)))
  1030.         (let ((other-window-scroll-buffer rmail-buffer))
  1031.           (scroll-other-window dist)))
  1032.     ;; This forces rmail-buffer to be sized correctly later.
  1033.     (display-buffer rmail-buffer)
  1034.     (setq rmail-current-message nil)))))
  1035.  
  1036. (defun rmail-summary-scroll-msg-down (&optional dist)
  1037.   "Scroll the Rmail window backward.
  1038. If the Rmail window is displaying the beginning of a message,
  1039. advance to the previous message."
  1040.   (interactive "P")
  1041.   (if (eq dist '-)
  1042.       (rmail-summary-scroll-msg-up nil)
  1043.     (let ((rmail-buffer-window (get-buffer-window rmail-buffer)))
  1044.       (if rmail-buffer-window
  1045.       (if (let ((rmail-summary-window (selected-window)))
  1046.         (select-window rmail-buffer-window)
  1047.         (prog1
  1048.             ;; Is BOB visible in the buffer?
  1049.             (save-excursion
  1050.               (move-to-window-line 0)
  1051.               (beginning-of-line)
  1052.               (bobp))
  1053.           (select-window rmail-summary-window)))
  1054.           (if (not rmail-summary-scroll-between-messages)
  1055.           (error "Beginning of buffer")
  1056.         (rmail-summary-previous-msg (or dist 1)))
  1057.         (let ((other-window-scroll-buffer rmail-buffer))
  1058.           (scroll-other-window-down dist)))
  1059.     ;; This forces rmail-buffer to be sized correctly later.
  1060.     (display-buffer rmail-buffer)
  1061.     (setq rmail-current-message nil)))))
  1062.  
  1063. (defun rmail-summary-beginning-of-message ()
  1064.   "Show current message from the beginning."
  1065.   (interactive)
  1066.   (pop-to-buffer rmail-buffer)
  1067.   (beginning-of-buffer)
  1068.   (pop-to-buffer rmail-summary-buffer))
  1069.  
  1070. (defun rmail-summary-bury ()
  1071.   "Bury the Rmail buffer and the Rmail summary buffer."
  1072.   (interactive)
  1073.   (let ((buffer-to-bury (current-buffer)))
  1074.     (let (window)
  1075.       (while (setq window (get-buffer-window rmail-buffer))
  1076.     (set-window-buffer window (other-buffer rmail-buffer)))
  1077.       (bury-buffer rmail-buffer))
  1078.     (switch-to-buffer (other-buffer buffer-to-bury))
  1079.     (bury-buffer buffer-to-bury)))
  1080.  
  1081. (defun rmail-summary-quit ()
  1082.   "Quit out of Rmail and Rmail summary."
  1083.   (interactive)
  1084.   (rmail-summary-wipe)
  1085.   (rmail-quit))
  1086.  
  1087. (defun rmail-summary-wipe ()
  1088.   "Kill and wipe away Rmail summary, remaining within Rmail."
  1089.   (interactive)
  1090.   (save-excursion (set-buffer rmail-buffer) (setq rmail-summary-buffer nil))
  1091.   (let ((local-rmail-buffer rmail-buffer))
  1092.     (kill-buffer (current-buffer))
  1093.     ;; Delete window if not only one.
  1094.     (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
  1095.     (delete-window))
  1096.     ;; Switch windows to the rmail buffer, or switch to it in this window.
  1097.     (pop-to-buffer local-rmail-buffer)))
  1098.  
  1099. (defun rmail-summary-expunge ()
  1100.   "Actually erase all deleted messages and recompute summary headers."
  1101.   (interactive)
  1102.   (save-excursion
  1103.     (set-buffer rmail-buffer)
  1104.     (rmail-only-expunge))
  1105.   (rmail-update-summary))
  1106.  
  1107. (defun rmail-summary-expunge-and-save ()
  1108.   "Expunge and save RMAIL file."
  1109.   (interactive)
  1110.   (save-excursion
  1111.     (set-buffer rmail-buffer)
  1112.     (rmail-only-expunge))
  1113.   (rmail-update-summary)
  1114.   (save-excursion
  1115.     (set-buffer rmail-buffer)
  1116.     (save-buffer))
  1117.   (set-buffer-modified-p nil))
  1118.  
  1119. (defun rmail-summary-get-new-mail ()
  1120.   "Get new mail and recompute summary headers."
  1121.   (interactive)
  1122.   (let (msg)
  1123.     (save-excursion
  1124.       (set-buffer rmail-buffer)
  1125.       (rmail-get-new-mail)
  1126.       ;; Get the proper new message number.
  1127.       (setq msg rmail-current-message))
  1128.     ;; Make sure that message is displayed.
  1129.     (or (zerop msg)
  1130.     (rmail-summary-goto-msg msg))))
  1131.  
  1132. (defun rmail-summary-input (filename)
  1133.   "Run Rmail on file FILENAME."
  1134.   (interactive "FRun rmail on RMAIL file: ")
  1135.   ;; We switch windows here, then display the other Rmail file there.
  1136.   (pop-to-buffer rmail-buffer)
  1137.   (rmail filename))
  1138.  
  1139. (defun rmail-summary-first-message ()
  1140.   "Show first message in Rmail file from summary buffer."
  1141.   (interactive)
  1142.   (beginning-of-buffer))
  1143.  
  1144. (defun rmail-summary-last-message ()
  1145.   "Show last message in Rmail file from summary buffer."
  1146.   (interactive)
  1147.   (end-of-buffer)
  1148.   (forward-line -1))
  1149.  
  1150. (defvar rmail-summary-edit-map nil)
  1151. (if rmail-summary-edit-map
  1152.     nil
  1153.   (setq rmail-summary-edit-map
  1154.     (nconc (make-sparse-keymap) text-mode-map))
  1155.   (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
  1156.   (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))
  1157.  
  1158. (defun rmail-summary-edit-current-message ()
  1159.   "Edit the contents of this message."
  1160.   (interactive)
  1161.   (pop-to-buffer rmail-buffer)
  1162.   (rmail-edit-current-message)
  1163.   (use-local-map rmail-summary-edit-map))
  1164.  
  1165. (defun rmail-summary-cease-edit ()
  1166.   "Finish editing message, then go back to Rmail summary buffer."
  1167.   (interactive)
  1168.   (rmail-cease-edit)
  1169.   (pop-to-buffer rmail-summary-buffer))
  1170.  
  1171. (defun rmail-summary-abort-edit ()
  1172.   "Abort edit of current message; restore original contents.
  1173. Go back to summary buffer."
  1174.   (interactive)
  1175.   (rmail-abort-edit)
  1176.   (pop-to-buffer rmail-summary-buffer))
  1177.  
  1178. (defun rmail-summary-search-backward (regexp &optional n)
  1179.   "Show message containing next match for REGEXP.
  1180. Prefix argument gives repeat count; negative argument means search
  1181. backwards (through earlier messages).
  1182. Interactively, empty argument means use same regexp used last time."
  1183.   (interactive
  1184.     (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
  1185.        (prompt
  1186.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1187.        regexp)
  1188.       (if rmail-search-last-regexp
  1189.       (setq prompt (concat prompt
  1190.                    "(default "
  1191.                    rmail-search-last-regexp
  1192.                    ") ")))
  1193.       (setq regexp (read-string prompt))
  1194.       (cond ((not (equal regexp ""))
  1195.          (setq rmail-search-last-regexp regexp))
  1196.         ((not rmail-search-last-regexp)
  1197.          (error "No previous Rmail search string")))
  1198.       (list rmail-search-last-regexp
  1199.         (prefix-numeric-value current-prefix-arg))))
  1200.   ;; Don't use save-excursion because that prevents point from moving
  1201.   ;; properly in the summary buffer.
  1202.   (let ((buffer (current-buffer)))
  1203.     (unwind-protect
  1204.     (progn
  1205.       (set-buffer rmail-buffer)
  1206.       (rmail-search regexp (- n)))
  1207.       (set-buffer buffer))))
  1208.  
  1209. (defun rmail-summary-search (regexp &optional n)
  1210.   "Show message containing next match for REGEXP.
  1211. Prefix argument gives repeat count; negative argument means search
  1212. backwards (through earlier messages).
  1213. Interactively, empty argument means use same regexp used last time."
  1214.   (interactive
  1215.     (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
  1216.        (prompt
  1217.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1218.        regexp)
  1219.       (if rmail-search-last-regexp
  1220.       (setq prompt (concat prompt
  1221.                    "(default "
  1222.                    rmail-search-last-regexp
  1223.                    ") ")))
  1224.       (setq regexp (read-string prompt))
  1225.       (cond ((not (equal regexp ""))
  1226.          (setq rmail-search-last-regexp regexp))
  1227.         ((not rmail-search-last-regexp)
  1228.          (error "No previous Rmail search string")))
  1229.       (list rmail-search-last-regexp
  1230.         (prefix-numeric-value current-prefix-arg))))
  1231.   ;; Don't use save-excursion because that prevents point from moving
  1232.   ;; properly in the summary buffer.
  1233.   (let ((buffer (current-buffer)))
  1234.     (unwind-protect
  1235.     (progn
  1236.       (set-buffer rmail-buffer)
  1237.       (rmail-search regexp n))
  1238.       (set-buffer buffer))))
  1239.  
  1240. (defun rmail-summary-toggle-header ()
  1241.   "Show original message header if pruned header currently shown, or vice versa."
  1242.   (interactive)
  1243.   (save-excursion
  1244.     (set-buffer rmail-buffer)
  1245.     (rmail-toggle-header))
  1246.   ;; Inside save-excursion, some changes to point in the RMAIL buffer are lost.
  1247.   ;; Set point to point-min in the RMAIL buffer, if it is visible.
  1248.   (let ((window (get-buffer-window rmail-buffer)))
  1249.     (if window
  1250.         ;; Using save-window-excursion would lose the new value of point.
  1251.         (let ((owin (selected-window)))
  1252.           (unwind-protect
  1253.               (progn
  1254.                 (select-window window)
  1255.                 (goto-char (point-min)))
  1256.             (select-window owin))))))
  1257.  
  1258.  
  1259. (defun rmail-summary-add-label (label)
  1260.   "Add LABEL to labels associated with current Rmail message.
  1261. Completion is performed over known labels when reading."
  1262.   (interactive (list (save-excursion
  1263.                (set-buffer rmail-buffer)
  1264.                (rmail-read-label "Add label"))))
  1265.   (save-excursion
  1266.     (set-buffer rmail-buffer)
  1267.     (rmail-add-label label)))
  1268.  
  1269. (defun rmail-summary-kill-label (label)
  1270.   "Remove LABEL from labels associated with current Rmail message.
  1271. Completion is performed over known labels when reading."
  1272.   (interactive (list (save-excursion
  1273.                (set-buffer rmail-buffer)
  1274.                (rmail-read-label "Kill label"))))
  1275.   (save-excursion
  1276.     (set-buffer rmail-buffer)
  1277.     (rmail-set-label label nil)))
  1278.  
  1279. ;;;; *** Rmail Summary Mailing Commands ***
  1280.  
  1281. (defun rmail-summary-mail ()
  1282.   "Send mail in another window.
  1283. While composing the message, use \\[mail-yank-original] to yank the
  1284. original message into it."
  1285.   (interactive)
  1286.   (let ((window (get-buffer-window rmail-buffer)))
  1287.     (if window
  1288.     (select-window window)
  1289.       (set-buffer rmail-buffer)))
  1290.   (rmail-start-mail nil nil nil nil nil (current-buffer))
  1291.   (use-local-map (copy-keymap (current-local-map)))
  1292.   (define-key (current-local-map)
  1293.     "\C-c\C-c" 'rmail-summary-send-and-exit))
  1294.  
  1295. (defun rmail-summary-continue ()
  1296.   "Continue composing outgoing message previously being composed."
  1297.   (interactive)
  1298.   (let ((window (get-buffer-window rmail-buffer)))
  1299.     (if window
  1300.     (select-window window)
  1301.       (set-buffer rmail-buffer)))
  1302.   (rmail-start-mail t))
  1303.  
  1304. (defun rmail-summary-reply (just-sender)
  1305.   "Reply to the current message.
  1306. Normally include CC: to all other recipients of original message;
  1307. prefix argument means ignore them.  While composing the reply,
  1308. use \\[mail-yank-original] to yank the original message into it."
  1309.   (interactive "P")
  1310.   (let ((window (get-buffer-window rmail-buffer)))
  1311.     (if window
  1312.     (select-window window)
  1313.       (set-buffer rmail-buffer)))
  1314.   (rmail-reply just-sender)
  1315.   (use-local-map (copy-keymap (current-local-map)))
  1316.   (define-key (current-local-map)
  1317.     "\C-c\C-c" 'rmail-summary-send-and-exit))
  1318.  
  1319. (defun rmail-summary-retry-failure ()
  1320.   "Edit a mail message which is based on the contents of the current message.
  1321. For a message rejected by the mail system, extract the interesting headers and
  1322. the body of the original message; otherwise copy the current message."
  1323.   (interactive)
  1324.   (let ((window (get-buffer-window rmail-buffer)))
  1325.     (if window
  1326.     (select-window window)
  1327.       (set-buffer rmail-buffer)))
  1328.   (rmail-retry-failure)
  1329.   (use-local-map (copy-keymap (current-local-map)))
  1330.   (define-key (current-local-map)
  1331.     "\C-c\C-c" 'rmail-summary-send-and-exit))
  1332.  
  1333. (defun rmail-summary-send-and-exit ()
  1334.   "Send mail reply and return to summary buffer."
  1335.   (interactive)
  1336.   (mail-send-and-exit t))
  1337.  
  1338. (defun rmail-summary-forward (resend)
  1339.   "Forward the current message to another user.
  1340. With prefix argument, \"resend\" the message instead of forwarding it;
  1341. see the documentation of `rmail-resend'."
  1342.   (interactive "P")
  1343.   (save-excursion
  1344.     (let ((window (get-buffer-window rmail-buffer)))
  1345.       (if window
  1346.       (select-window window)
  1347.     (set-buffer rmail-buffer)))
  1348.     (rmail-forward resend)
  1349.     (use-local-map (copy-keymap (current-local-map)))
  1350.     (define-key (current-local-map)
  1351.       "\C-c\C-c" 'rmail-summary-send-and-exit)))
  1352.  
  1353. (defun rmail-summary-resend ()
  1354.   "Resend current message using 'rmail-resend'."
  1355.   (interactive)
  1356.   (save-excursion
  1357.     (let ((window (get-buffer-window rmail-buffer)))
  1358.       (if window
  1359.       (select-window window)
  1360.     (set-buffer rmail-buffer)))
  1361.     (call-interactively 'rmail-resend)))
  1362.  
  1363. ;; Summary output commands.
  1364.  
  1365. (defun rmail-summary-output-to-rmail-file (&optional file-name)
  1366.   "Append the current message to an Rmail file named FILE-NAME.
  1367. If the file does not exist, ask if it should be created.
  1368. If file is being visited, the message is appended to the Emacs
  1369. buffer visiting that file."
  1370.   (interactive)
  1371.   (save-excursion
  1372.     (set-buffer rmail-buffer)
  1373.     (let ((rmail-delete-after-output nil))
  1374.       (if file-name
  1375.       (rmail-output-to-rmail-file file-name)
  1376.     (call-interactively 'rmail-output-to-rmail-file))))
  1377.   (if rmail-delete-after-output
  1378.       (rmail-summary-delete-forward nil)))
  1379.  
  1380. (defun rmail-summary-output-menu ()
  1381.   "Output current message to another Rmail file, chosen with a menu.
  1382. Also set the default for subsequent \\[rmail-output-to-rmail-file] commands.
  1383. The variables `rmail-secondary-file-directory' and
  1384. `rmail-secondary-file-regexp' control which files are offered in the menu."
  1385.   (interactive)
  1386.   (save-excursion
  1387.     (set-buffer rmail-buffer)
  1388.     (let ((rmail-delete-after-output nil))
  1389.       (call-interactively 'rmail-output-menu)))
  1390.   (if rmail-delete-after-output
  1391.       (rmail-summary-delete-forward nil)))
  1392.  
  1393. (defun rmail-summary-output ()
  1394.   "Append this message to Unix mail file named FILE-NAME."
  1395.   (interactive)
  1396.   (save-excursion
  1397.     (set-buffer rmail-buffer)
  1398.     (let ((rmail-delete-after-output nil))
  1399.       (call-interactively 'rmail-output)))
  1400.   (if rmail-delete-after-output
  1401.       (rmail-summary-delete-forward nil)))
  1402.  
  1403. (defun rmail-summary-construct-io-menu ()
  1404.   (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
  1405.     (if files
  1406.     (progn
  1407.       (define-key rmail-summary-mode-map [menu-bar classify input-menu]
  1408.         (cons "Input Rmail File" 
  1409.           (rmail-list-to-menu "Input Rmail File" 
  1410.                       files
  1411.                       'rmail-summary-input)))
  1412.       (define-key rmail-summary-mode-map [menu-bar classify output-menu]
  1413.         (cons "Output Rmail File" 
  1414.           (rmail-list-to-menu "Output Rmail File" 
  1415.                       files
  1416.                       'rmail-summary-output-to-rmail-file))))
  1417.       (define-key rmail-summary-mode-map [menu-bar classify input-menu]
  1418.     '("Input Rmail File" . rmail-disable-menu))
  1419.       (define-key rmail-summary-mode-map [menu-bar classify output-menu]
  1420.     '("Output Rmail File" . rmail-disable-menu)))))
  1421.  
  1422.  
  1423. ;; Sorting messages in Rmail Summary buffer.
  1424.  
  1425. (defun rmail-summary-sort-by-date (reverse)
  1426.   "Sort messages of current Rmail summary by date.
  1427. If prefix argument REVERSE is non-nil, sort them in reverse order."
  1428.   (interactive "P")
  1429.   (rmail-sort-from-summary (function rmail-sort-by-date) reverse))
  1430.  
  1431. (defun rmail-summary-sort-by-subject (reverse)
  1432.   "Sort messages of current Rmail summary by subject.
  1433. If prefix argument REVERSE is non-nil, sort them in reverse order."
  1434.   (interactive "P")
  1435.   (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))
  1436.  
  1437. (defun rmail-summary-sort-by-author (reverse)
  1438.   "Sort messages of current Rmail summary by author.
  1439. If prefix argument REVERSE is non-nil, sort them in reverse order."
  1440.   (interactive "P")
  1441.   (rmail-sort-from-summary (function rmail-sort-by-author) reverse))
  1442.  
  1443. (defun rmail-summary-sort-by-recipient (reverse)
  1444.   "Sort messages of current Rmail summary by recipient.
  1445. If prefix argument REVERSE is non-nil, sort them in reverse order."
  1446.   (interactive "P")
  1447.   (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))
  1448.  
  1449. (defun rmail-summary-sort-by-correspondent (reverse)
  1450.   "Sort messages of current Rmail summary by other correspondent.
  1451. If prefix argument REVERSE is non-nil, sort them in reverse order."
  1452.   (interactive "P")
  1453.   (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))
  1454.  
  1455. (defun rmail-summary-sort-by-lines (reverse)
  1456.   "Sort messages of current Rmail summary by lines of the message.
  1457. If prefix argument REVERSE is non-nil, sort them in reverse order."
  1458.   (interactive "P")
  1459.   (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))
  1460.  
  1461. (defun rmail-summary-sort-by-keywords (reverse labels)
  1462.   "Sort messages of current Rmail summary by keywords.
  1463. If prefix argument REVERSE is non-nil, sort them in reverse order.
  1464. KEYWORDS is a comma-separated list of labels."
  1465.   (interactive "P\nsSort by labels: ")
  1466.   (rmail-sort-from-summary
  1467.    (function (lambda (reverse)
  1468.            (rmail-sort-by-keywords reverse labels)))
  1469.    reverse))
  1470.  
  1471. (defun rmail-sort-from-summary (sortfun reverse)
  1472.   "Sort Rmail messages from Summary buffer and update it after sorting."
  1473.   (require 'rmailsort)
  1474.   (let ((selwin (selected-window)))
  1475.     (unwind-protect
  1476.     (progn (pop-to-buffer rmail-buffer)
  1477.            (funcall sortfun reverse))
  1478.       (select-window selwin))))
  1479.  
  1480. ;;; rmailsum.el ends here
  1481.